<!-- 公区能源费新增 -->
<template>
	<el-drawer v-model="isShowDrawer" direction="rtl" :before-close="handleClose" :size="curDrawerSize" :close-on-click-modal="isReadOnly">
		<template #header="{ titleId, titleClass }">
			<div class="zs-drawer-header" :id="titleId" :class="titleClass">
				<i class="icon-b-item zs-icon zs-text-blue zs-space-right-sm"></i>
				<div class="zs-title zs-text-black">清单名称</div>
			</div>
		</template>

		<template #default>
			<el-container class="zs-drawer-container">
				<el-scrollbar class="zs-drawer-content">
					<!-- card1 -->
					<div class="zs-align-center zs-space-bottom" id="sm-drawer-demo-card-01">
						<i class="icon-s-item zs-icon zs-text-blue zs-space-right-sm"></i>
						<div class="zs-title">清单名称</div>
					</div>
					<div class="zs-card zs-space-bottom">
						<el-form ref="form1" label-position="right" label-width="100px" :model="queryParams.data">
							<el-form-item label="清单名称">
								<el-select v-model="queryParams.data.inventoryId" class="zs-select-md zs-space-right-sm" placeholder="清单名称" filterable clearable @change="queryList">
									<el-option v-for="item in inventoryList" :key="item.inventoryId" :label="item.inventoryName" :value="item.inventoryId" />
								</el-select>
							</el-form-item>
						</el-form>
					</div>

					<div class="zs-align-center zs-space-bottom" id="sm-drawer-demo-card-02">
						<i class="icon-s-item zs-icon zs-text-blue zs-space-right-sm"></i>
						<div class="zs-title">设备信息</div>
					</div>
					<div class="zs-card zs-space-bottom">
						<div class="zs-card-sm zs-layout-flex-row zs-layout-justify-between">
							<span class="zs-layout-flex-row">
								<el-input v-model="queryParams.data.deviceName" class="zs-space-right-sm" placeholder="设备名称" clearable @blur="queryList" />
								<el-input v-model="queryParams.data.deviceNum" class="zs-space-right-sm" placeholder="设备编号" clearable @blur="queryList" />
								<el-input v-model="queryParams.data.deviceLocation" class="zs-space-right-sm" placeholder="所在位置" clearable @blur="queryList" />
							</span>
							<el-button class="zs-btn-plain" @click="queryList"> 查询 </el-button>
						</div>

						<!-- teble -->
						<div class="zs-card zs-table-page-content zs-layout-flex-auto">
							<el-table ref="tableRef" row-key="id" :data="tableData.data" v-loading="tableData.loading" stripe @selection-change="handleSelectionChange">
								<el-table-column type="selection" width="55" />
								<el-table-column type="index" label="序号" width="60" align="center" />
								<el-table-column prop="deviceName" label="设备名称" align="left" show-overflow-tooltip />
								<el-table-column prop="deviceNum" label="设备编号" align="left" show-overflow-tooltip />
								<el-table-column prop="deviceLocation" label="所在位置" align="left" show-overflow-tooltip />
							</el-table>
						</div>
					</div>
				</el-scrollbar>
				<hsDocWrapper :nodeList="curNodeList" />
			</el-container>
		</template>

		<template #footer>
			<div>
				<el-button class="zs-btn-md zs-btn-primary" @click="doConfirm">提交</el-button>
			</div>
		</template>
	</el-drawer>
</template>

<script setup>
	import { deviceListApi, publicAreaAddApi, inventoryListByProjectApi } from '@/api/energy.js';
	import { ElMessage } from 'element-plus';
	import { ref, reactive, watch, nextTick } from 'vue';
	import { useRoute } from 'vue-router';
	import { useStore } from 'vuex';

	const route = useRoute();
	let store = useStore();
	const curDrawerSize = ref(store.state.uiDrawerSizeBig);

	const props = defineProps({
		isShow: { type: Boolean, default: false },
		isReadOnly: { type: Boolean, default: true },
		handleType: { type: String, default: '' },
		currentRow: { type: Object },
		standradTypeList: { type: Array },
		pointTypeList: { type: Array },
	});

	const tableRef = ref();

	const emit = defineEmits(['close']);

	let isShowDrawer = ref(props.isShow);

	//查询条件
	let queryParams = reactive({ data: {} });

	// 列表 filed
	let tableData = reactive({
		loading: false,
		data: [],
	});

	const curNodeList = ref([
		{ name: '清单名称', id: 'sm-drawer-demo-card-01' },
		{ name: '设备信息', id: 'sm-drawer-demo-card-02' },
	]);

	watch(
		() => props.isShow,
		(newVal, oldVal) => {
			isShowDrawer.value = newVal;
			nextTick(() => {
				newVal && props.handleType == 'add' && initForm();
			});
		},
	);

	//列表
	const queryList = async () => {
		tableData.loading = true;
		let res = await deviceListApi(queryParams.data);
		if (res.data.code !== 200) return;
		tableData.loading = false;
		tableData.data = res.data.data || [];
	};

	//提交
	const doConfirm = async () => {
		let ids = chooseTagList.value.map(item => item.id);
		let params = { deviceIdList: ids, projectCode: route.query.projectCode };
		let res = await publicAreaAddApi(params);
		if (res.data.code !== 200) return;
		ElMessage({
			message: '提交成功',
			type: 'success',
		});
		isShowDrawer.value = false;
		emit('close', { isShow: false, isRefresh: true });
	};

	//选择
	let chooseTagList = ref([]);
	const handleSelectionChange = data => {
		chooseTagList.value = data;
	};

	//关闭抽屉
	const handleClose = done => {
		emit('close', { isShow: false });
		done();
	};

	//表单初始化
	const initForm = () => {
		queryParams.data = { projectCode: route.query.projectCode, inventoryId: '', deviceName: '', deviceNum: '', deviceLocation: '' };
		tableData.data = [];
	};

	//清单名称
	let inventoryList = ref([]);
	const getInventoryList = async () => {
		let res = await inventoryListByProjectApi({ projectCode: route.query.projectCode });
		inventoryList.value = res.data.data;
	};

	getInventoryList()
</script>
